
layui.use(['form','upload','table','wangEditor','laydate'], function () {
    let form = layui.form,
        layer = layui.layer,
        upload = layui.upload,
        laydate = layui.laydate,
        $ = layui.$,
        additem_index=0
        ,getuploadrender=function (upload_data) {
            let upload_render={
                elem: upload_data.headImg,
                url: '/admin/adverbanner/Doupload',
                size: 500,
                field: upload_data.thumb_img,
                accept: 'images',
                acceptMime: 'image/*',
                before: function(obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result) {
                        $(upload_data.imgurl).attr('src', result); //图片链接（base64）;
                    });

                    this.data = {
                        'mark': 'img',
                        'field':upload_data.thumb_img,
                        'oldSrc': $(upload_data.imgurlHidden).val()
                    };

                },
                done: function(res) {

                    //如果上传失败
                    if (res.status == false) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $(upload_data.imgurlHidden).val(res.src);
                    let demoText = $(upload_data.demoText);
                    demoText.html('<span style="color: #8f8f8f;">上传成功!!!</span>');
                },
                error: function(res) {
                    //演示失败状态，并实现重传
                    let demoText = $(upload_data.demoText);
                    demoText.html('<span style="color: #FF5722;">上传失败</span>');

                },

            }
            return upload_render;
        };

    //单图片拖拽上传:缩略图
    let upload_render=getuploadrender({headImg:'#headImg',imgurl:'#imgurl',imgurlHidden:'#imgurlHidden',demoText:'#demoText',thumb_img:'thumb_img'})
    upload.render(upload_render);
    // 开始结束时间
    //日期
    laydate.render({
        elem: '#start_time'
        ,range: true //或 range: '~' 来自定义分割字符
        ,type:'datetime'
    });
    // laydate.render({elem: '#end_time'});
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        let  postdata =data.field

        //发异步，把数据提交给php
        $.ajax({
            url: '/admin/adverbanner/Save?action=ajax',
            type: 'post',
            dataType: 'json',
            data:postdata,
            success: function(res) {


                if (res.status == true) {
                    layer.msg('保存成功', {icon: 1, time: 1000},
                        function(){
                            parent.location.reload();
                        })

                }  else {
                    layer.open({
                        title: '错误信息',
                        content: '<span style="color:red">'+res.code+':</span>'+res.msg
                    })
                    return false
                }
            },
            error: function(re) {

                layer.open({
                    title: '错误信息',
                    content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                })
            }
        });




    });

    //监听指定开关
    form.on('switch(switchTest)', function(data){

        if(data.value==1)
        {
            //关闭操作
            $("#bizpro_type").attr('style','display:none')
            $(this).val(2)

        }else{
            $("#bizpro_type").attr('style','margin-left:35px')
            $(this).val(1)
        }


        //$(".additem").attr('style','')



    });

    //监听添加关联商品
    $(document).on('click','.additem',function(){
        additem_index=additem()
    });
    function additem() {
        let index=0
        let re= $.ajax({
            url: '/admin/adverbanner/bizpro' ,
            type: 'post',
            dataType: 'json',
            async : false,
            success: function(data) {
                index=layer.open({
                    type: 1,
                    title: '选择商品',
                    resize: false,
                    move: false,
                    skin: 'layui-layer-rim',
                    area: ['500px', '500px'],
                    content: data.info
                });


            },
            error: function(re) {
                index=layer.msg("连接失败,报错了");
            }
        });
        return index


    }

});

/* 选定商品*/
function ChooseInfo(obj) {

    //名称
    let name=$(obj).parents('tr').find('td').eq(1).html()
    //售价
    let sale_price=$(obj).parents('tr').find('td').eq(2).html()
    //编号
    let code=$(obj).parents('tr').find('td').eq(3).html()
    //组件字符串
    let new_html='';
    new_html+='<table class="layui-table"  style="width:80%;margin-left: 150px;">\n' +
        '        <colgroup>\n' +
        '            <col width="150">\n' +
        '            <col width="200">\n' +
        '            <col width="200">\n' +
        '            <col width="150">\n' +
        '        </colgroup>\n' +
        '        <thead>\n' +
        '        <tr>\n' +
        '            <th>名称</th>\n' +
        '            <th>售价</th>\n' +
        '            <th>商品编号</th>\n' +
        '            <th>操作</th>\n' +
        '        </tr>\n' +
        '        </thead>\n' +
        '        <tbody>\n' +
        '\n' +
        '        <tr>\n' +
        '            <td>'+name+'</td>\n' +
        '            <td>'+sale_price+'</td>\n' +
        '            <td>'+code+'</td>\n' +
        '            <td><button type="button"  lay-event="additem"  class="layui-btn additem">修改</button></td>\n' +
        '        </tr>\n' +
        '        </tbody>\n' +
        '    </table>'

    //$('#bizpro_type').append(new_html);
    if($('#bizpro_type').find('.layui-table').length<1)
    {
        $('#bizpro_type').append(new_html);
    }else{
        $('#bizpro_type').find('.layui-table').remove();
        $('#bizpro_type').append(new_html)
    }

    $(".additem").attr('style','diplay:none')
    //修改bizpro_id值
    $('input[name="bizpro_id"]').val($(obj).parents('tr').attr('id').substr(6))
    //关闭最新弹出层
    layer.close(layer.index)


}






